package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    public final void start() {

        loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    removeStudent();
                    break;
                case 3:
                    changeStudent();
                    break;
                case 4:
                    findStudent();
                    break;
                case 5:
                    System.out.println("欢迎下次使用学生信息管理系统，再见！");
                    break loop;
                default:
                    System.out.println("输入有误，请重新输入：");
                    break;
            }
        }
    }

    private final void changeStudent() {
        Student[] stu = studentService.findAll();
        String id;
        if (stu == null) {
            System.out.println("查无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入修改的学号");
            id=sc.next();
            boolean ss = studentService.affirm(id);
            if (ss){
                break;
            }else{
                System.out.println("学号输入有误，请重新输入：");
            }
        }
        System.out.println("请输入需要修改的姓名：");
        String name = sc.next();
        System.out.println("请输入需要修改的年龄：");
        String age = sc.next();
        System.out.println("请输入需要修改的生日：");
        String birth = sc.next();
        boolean change=studentService.change(id,name,age,birth);
        if (change){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    private final void removeStudent() {
        Student[] stu = studentService.findAll();
        String id;
        if (stu == null) {
            System.out.println("查无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入您要删除的学号：");
            id = sc.next();
            boolean ss = studentService.affirm(id);
            if (ss){
                break;
            }else{
                System.out.println("学号输入有误，请重新输入：");
            }
        }
        boolean studentRemove=studentService.removeStudent(id);
        if (studentRemove){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }

    }

    public final void findStudent() {
        Student[] stu = studentService.findAll();
        if (stu == null) {
            System.out.println("查无信息，请先添加");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null) {
                System.out.println(stu[i].getID() + "\t" + stu[i].getName() + "\t" + stu[i].getAge() + "\t\t" + stu[i].getBirthday());
            }
        }
    }

    public abstract void addStudent();
}
